/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2022-04-30 11:10:44
 * @LastEditors: ding-cx
 * @LastEditTime: 2022-06-08 10:18:33
 * @Description: 钉钉考勤接口调用
 */

import DingTalkBasePublicService from "./BasePublicService";
import { Dingtalk } from "../dingtalk";

export default class AttService extends DingTalkBasePublicService {
	/** 获取打卡结果  钉钉按照排班处理过的结果，不含全部打卡记录*/
	async getAttendanceList(params: Dingtalk.ov.IRequestAttendanceDaKaResultList) {
		// todo validate the params
		const url = await this.getURL(`https://oapi.dingtalk.com/attendance/list`);
		const method = "POST";

		const data = params;
		const res = await this.curl<Dingtalk.ov.IResponseAttendanceDaKaResultList>(url, {
			contentType: "json",
			dataType: "json",
			method,
			data,
		});
		this.ovcheckError(url, res);
		return res.data;
	}

	/**
	 * 查询单个人，单天的打卡结果
	 *
	 * @param {string} userId
	 * @param {string} yymmdd 日期 形如yyyy-MM-dd
	 * @return {*}
	 * @memberof PlatformDingTalkBaseKaoQinService
	 */
	async getOneUserOneDayAttendance(userId: string, yymmdd: string) {
		const params: Dingtalk.ov.IRequestAttendanceDaKaResultList = {
			workDateFrom: `${yymmdd} 00:00:00`,
			workDateTo: `${yymmdd} 23:59:59`,
			userIdList: [userId],
			offset: 0,
			limit: 50,
		};
		return await this.getAttendanceList(params);
	}

	/** 获取打卡详情 包含原始刷卡记录
	 * - http://open.dingtalk.com/document/orgapp-server/attendance-clock-in-record-is-open
	 */
	async getAttendanceOriginRecord(params: Dingtalk.ov.IRequestAttendanceOriginRecord) {
		const url = await this.getURL(`https://oapi.dingtalk.com/attendance/listRecord`);
		const data = params;
		const res = await this.curl<Dingtalk.ov.IResponseAttendanceOriginRecord>(url, {
			contentType: "json",
			dataType: "json",
			method: "POST",
			data,
		});
		this.ovcheckError(url, res);
		return res.data;
	}
}
